package com.kingdee.pda.ui.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.example.basecommon.utils.SpeakUtils;
import com.kingdee.pda.R;
import com.kingdee.pda.entity.ServerBean;
import com.kingdee.pda.util.CommonUtil;


/**
 * 带有按钮回调的对话框
 */

public class ServerAddressDialog {

    /**
     * 带message的弹框
     */
    public static void showInputAddressDialog(String title, String url, final Context activity, final InputListener listener) {
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_input_server_address, null);
        final EditText editInputTitle = (EditText) view.findViewById(R.id.etServerTitle);
        final EditText editInput = (EditText) view.findViewById(R.id.etServerAddress);
        editInputTitle.setText(title);
        editInput.setText(url);
        AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.base_common_AlertDialogStyle);
        builder.setTitle("自定义服务器地址");
        builder.setView(view);
        builder.setCancelable(false);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("确定", null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String title = editInputTitle.getText().toString();
                String inputServerAddress = editInput.getText().toString().replace(" ", "");

                if (TextUtils.isEmpty(title) || TextUtils.isEmpty(inputServerAddress)) {
                    SpeakUtils.speakWithToast(activity, "服务器信息不能为空，请检查！", false, false);
                } else if (inputServerAddress.contains("：")) {
                    SpeakUtils.speakWithToast(activity, "输入了错误的冒号，请检查！", false, false);
                } else if (!CommonUtil.checkHttpUrl(inputServerAddress)) {
                    SpeakUtils.speakWithToast(activity, "服务器地址输入有误，请检查！", false, false);
                } else {
                    listener.okEvent(new ServerBean(title, inputServerAddress), alertDialog);
                }
            }
        });
    }

//    /**
//     * 带message的弹框
//     *
//     * @param activity
//     * @param title
//     * @param listener
//     */
//    public static AlertDialog showInputStockCodeDialog(final Activity activity, String title, final InputListener listener) {
//        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_input_server_address, null);
//        final EditText editInput = (EditText) view.findViewById(R.id.etServerAddress);
//
//        AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.base_common_AlertDialogStyle);
//        builder.setTitle(title);
//        builder.setView(view);
//        builder.setCancelable(false);
//        builder.setPositiveButton("确定", null);
//        final AlertDialog dialog = builder.show();
//        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String inputString = editInput.getText().toString().replace(" ", "");
//                if (TextUtils.isEmpty(inputString)) {
//                    SpeakUtils.speakWithToast(activity, "输入不能为空，请检查！", false, true);
//                } else {
//                    listener.okEvent(inputString, dialog);
//                }
//            }
//        });
//        return dialog;
//    }

    public interface InputListener {
        void okEvent(ServerBean serverAddress, DialogInterface dialog);
    }
}
